package com.leo.ibatis.demo.dao.ibatis;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.util.Assert;

import com.leo.ibatis.demo.criteria.BannerdataCriteria;
import com.leo.ibatis.demo.dao.BannerdataDao;
import com.leo.ibatis.demo.domain.Bannerdata;
import com.leo.ibatis.demo.util.Paging;

public class BannerdataDaoImpl extends SqlMapClientDaoSupport implements BannerdataDao {

	public void insert(Bannerdata bannerdata) throws DataAccessException {
		getSqlMapClientTemplate().insert("Bannerdata.insert", bannerdata);
	}

	public int update(Bannerdata bannerdata) throws DataAccessException {
		int actualRowsAffected = 1;

		getSqlMapClientTemplate().update("Bannerdata.update", bannerdata, actualRowsAffected);

		return actualRowsAffected;
	}

	public int delete(String favcategory) throws DataAccessException {
		int actualRowsAffected = 1;

		getSqlMapClientTemplate().delete("Bannerdata.delete", favcategory, actualRowsAffected);

		return actualRowsAffected;
	}

	public Bannerdata select(String favcategory) throws DataAccessException {
		return (Bannerdata) getSqlMapClientTemplate().queryForObject("Bannerdata.select", favcategory);
	}

	@SuppressWarnings("unchecked")
	public List<Bannerdata> list(BannerdataCriteria bannerdataCriteria) throws DataAccessException {
		Assert.notNull(bannerdataCriteria, "bannerdataCriteria must not be null");
		
		return getSqlMapClientTemplate().queryForList("Bannerdata.list", bannerdataCriteria);
	}

	@SuppressWarnings("unchecked")
	public List<Bannerdata> listOnPage(BannerdataCriteria bannerdataCriteria) throws DataAccessException {
		Assert.notNull(bannerdataCriteria, "bannerdataCriteria must not be null");
		Assert.notNull(bannerdataCriteria.getPaging(), "paging must not be null");
		
		int totalResults = count(bannerdataCriteria);
		Paging paging = bannerdataCriteria.getPaging();
		paging.setTotalResults(totalResults);
		
		return getSqlMapClientTemplate().queryForList("Bannerdata.list", bannerdataCriteria, paging.getFirstResult(), paging.getMaxResults());
	}

	public int count(BannerdataCriteria bannerdataCriteria) throws DataAccessException {
		Assert.notNull(bannerdataCriteria, "bannerdataCriteria must not be null");
		
		return ((Integer) getSqlMapClientTemplate().queryForObject("Bannerdata.count", bannerdataCriteria)).intValue();
	}

}
